package com.zwk.center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zwk.base.handler.GuLiException;
import com.zwk.base.util.JwtUtils;
import com.zwk.base.util.MD5;
import com.zwk.center.entity.UcenterMember;
import com.zwk.center.entity.vo.RegisterVo;
import com.zwk.center.mapper.UcenterMemberMapper;
import com.zwk.center.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author zwk
 * @since 2022-08-28
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private UcenterMemberMapper ucenterMemberMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String login(UcenterMember member) {
        String phone = member.getMobile();
        String password = member.getPassword();
        // 空值处理
        if (ObjectUtils.isEmpty(phone) || ObjectUtils.isEmpty(password)){
            throw new GuLiException(20001, "账号或密码不能为空");
        }

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", phone);
        UcenterMember ucenterMember = ucenterMemberMapper.selectOne(wrapper);
        if (ucenterMember == null){
            throw new GuLiException(20001, "请先注册");
        }

        // 判断密码, md5加密
        password = MD5.encrypt(password);
        if (!password.equals(ucenterMember.getPassword())){
            throw new GuLiException(20001, "密码错误");
        }

        // 判断是否封号
        if (ucenterMember.getIsDisabled()){
            throw new GuLiException(20001, "账号被封禁");
        }

        // 登录成功, 生成token
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String phone = registerVo.getMobile();
        String password = registerVo.getPassword();
        String nickname = registerVo.getNickname();
        String code = registerVo.getCode();
        // 空值处理
        if (ObjectUtils.isEmpty(phone) || ObjectUtils.isEmpty(password)
                || ObjectUtils.isEmpty(nickname) || ObjectUtils.isEmpty(code)){
            throw new GuLiException(20001, "数据不能为空");
        }

        // 判断验证码
        String redisCode = stringRedisTemplate.opsForValue().get(phone);
        if (ObjectUtils.isEmpty(redisCode) || !code.equals(redisCode)){
            throw new GuLiException(20001, "验证码错误");
        }

        // 判断账号是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", phone);
        UcenterMember ucenterMember = ucenterMemberMapper.selectOne(wrapper);
        if (ucenterMember != null){
            throw new GuLiException(20001, "该账号已经被注册");
        }

        // 注册成功, 数据放入数据库
        UcenterMember ucenterMember1 = new UcenterMember();
        ucenterMember1.setMobile(phone);
        ucenterMember1.setNickname(nickname);
        password = MD5.encrypt(password);
        ucenterMember1.setPassword(password);
        ucenterMember1.setIsDisabled(false);
        ucenterMember1.setAvatar("https://zwk-guli.oss-cn-hangzhou.aliyuncs.com/2022/08/07087beb32094015bbb147696d190ad9.png");

        ucenterMemberMapper.insert(ucenterMember1);

    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        return ucenterMemberMapper.selectOne(wrapper);
    }

    // 查询某一天的注册人数
    @Override
    public Integer countRegisterDay(String day) {
        return null;
    }
}
